package com.hmdp.config;

import com.hmdp.service.IUserService;
import com.hmdp.utils.interceptor.LoginInterceptor;
import com.hmdp.utils.interceptor.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * Mvc 的配置 - 拦截器
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(
                        "/shop-type/**",
                        "/blog/hot",
                        "/user/code",
                        "/user/login",
                        "/doc.html", "/webjars/**", "/swagger-resources", "/v2/api-docs").order(1);

        // token 刷新的拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate, userService)).addPathPatterns("/**").order(0);
    }
}
